<?php
/**
* 商品
*
* @copyright shopex.cn 2015.05.07
* @author sunjing<sunjing@shopex.cn>
*/
class middleware_wms_matrixwms_request_qimen_goods extends middleware_wms_matrixwms_request_goods{

    public function goods_add(&$sdf,$sync=false){

        $warehouse_code = '';
        if ($sdf[0]['branch_bn']) {
            $warehouse_code = $this->getWarehouse_code($sdf[0]['branch_bn']); 
        }

        foreach ((array) $sdf as $good ) {
            if (!$good['bn'] || !is_array($good)) continue;

            $params = $this->__get_params($good,$product_ids);

            $params['warehouse_code'] = $warehouse_code;

            $adapter_callback = array(
                'class' => __CLASS__,
                'method' => 'goods_add_callback',
            );
            $writelog = array(
                'log_title' => '商品添加',
                'log_type' => 'store.trade.goods',
                'addon' => $product_ids,
            );
            $method = 'store.wms.item.add';
            
            $response = $this->request($method,$params,$writelog,$sync,$adapter_callback);   

            // if ($response['rsp'] == 'succ' || $response['rsp'] == 'success') {

            //     $sync_sku_data[] = array(
            //         'inner_sku' => $good['bn'],
            //         // 'outer_sku' => $data['wms_item_code'],
            //         'status'    => '3',
            //         'new_tag'   => '1',
            //         'outer_sku' => $response['data']['succ'][0]['wms_item_code'] ? $response['data']['succ'][0]['wms_item_code'] : '',
            //     );

            //     $this->set_sync_status($sync_sku_data,$this->node_id);
            // } else {
            //     $sync_sku_data[] = array(
            //         'inner_sku' => $good['bn'],
            //         'status'    => '1',
            //     ); 

            //     $this->set_sync_status($sync_sku_data,$this->node_id);
            // }
        }
    }

    public function goods_update(&$sdf,$sync=false){

        $warehouse_code = '';
        if ($sdf[0]['branch_bn']) {
            $warehouse_code = $this->getWarehouse_code($sdf[0]['branch_bn']); 
        }

        foreach ((array) $sdf as $good ) {
            if (!$good['bn'] || !is_array($good)) continue;

            $params = $this->__get_params($good,$product_ids);
            $params['warehouse_code'] = $warehouse_code;

            $adapter_callback = array(
                'class' => __CLASS__,
                'method' => 'goods_add_callback',
            );
            $writelog = array(
                'log_title' => '商品更新',
                'log_type' => 'store.trade.goods',
                'addon' => $product_ids,
            );
            $method = 'store.wms.item.update';
            
            $response = $this->request($method,$params,$writelog,$sync,$adapter_callback);

            // if ($response['rsp'] == 'succ' || $response['rsp'] == 'success') {

            //     $sync_sku_data[] = array(
            //         'inner_sku' => $good['bn'],
            //         // 'outer_sku' => $data['wms_item_code'],
            //         'status'    => '3',
            //         'new_tag'   => '1',
            //         'outer_sku' => $response['data']['succ'][0]['wms_item_code'] ? $response['data']['succ'][0]['wms_item_code'] : '',
            //     );

            //     $this->set_sync_status($sync_sku_data,$this->node_id);
            // } else {
            //     $sync_sku_data[] = array(
            //         'inner_sku' => $good['bn'],
            //         'status'    => '4',
            //     ); 

            //     $this->set_sync_status($sync_sku_data,$this->node_id);
            // }
        }
    }

    /**
     * 商品同步参数
     * @param  array sdf
     * @return  array
     * @access  protected
     * @author sunjing@shopex.cn
     */
    protected function __get_params(&$p,&$product_ids=array()){

        $node_id = $this->node_id;

        $wms_id = kernel::single('channel_func')->getWmsIdByNodeId($node_id);

        $params = $items = array();

        $product_ids = array();

        $product_ids[] = $p['product_id'];
        $spec_info = preg_replace(array('/：/','/、/'),array(':',';'),$p['property']);
        $items[] = array(
            'name'                => $p['name'],
            'title'               => $p['name'],// 商品标题
            'item_code'           => $p['bn'],
            'remark'              => '',//商品备注
            'type'                => 'NORMAL',
            'is_sku'              => '1',
            'gross_weight'        => $p['weight'] ? $p['weight'] : '',// 毛重,单位G
            'net_weight'          => $p['weight'] ? $p['weight'] : '',// 商品净重,单位G
            'tare_weight'         => '',// 商品皮重，单位G
            'is_friable'          => '',// 是否易碎品
            'is_dangerous'        => '',// 是否危险品
            //'weight'            => $p['weight'] ? $p['weight'] : '0',
            //'length'            => '0.00',// 商品长度，单位厘米
            //'width'             => '0.00',// 商品宽度，单位厘米
            //'height'            => '0.00',// 商品高度，单位厘米
            //'volume'            => '0.00',// 商品体积，单位立方厘米
            'pricing_cat'         => '',// 计价货类
            'package_material'    => '',// 商品包装材料类型
            'price'               => '',
            'support_batch'       => '否',
            'support_expire_date' => '否',
            'expire_date'         => date('Y-m-d'),
            'support_barcode'     => '0',
            'barcode'             => $p['barcode'] ? $p['barcode'] : '',
            'support_antifake'    => '否',
            'unit'                => $p['unit'] ? $p['unit'] : '',
            'package_spec'        => '',// 商品包装规格
            'ename'               => '',// 商品英文名称
            'brand'               => '',
            'batch_no'            => '',
            'goods_cat'           => '',// 商品分类
            'color'               => '',// 商品颜色
            'property'            => $spec_info,//规格
            'item_id'       => kernel::single('console_foreignsku')->get_outer_sku($p['bn'],$wms_id),
        );
            
        $params['item_lists'] = json_encode(array('item'=>$items));
        $params['uniqid'] = self::uniqid();
        $params['to_version'] = '2.0';

        // $params['warehouse_code'] = 'KJ-0009';
        return $params;
    }
}